package Queue;

import javax.xml.bind.annotation.XmlAnyAttribute;

public class MyQueue {
    class Node {
        Node next;
        int val;

        public Node(int val) {
            this.val = val;
        }

        public Node() {}
    }

    static Node head;
    static Node rear;

    public void offer (int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
            rear = head;
        } else {
            rear.next = node;
            rear = node;
        }
    }

    public int poll () {
        if (head == null ) {
            throw new RuntimeException();
        } else {
            int ret = head.val;
            Node node = head;
            head = head.next;
            return ret;
        }
    }

    public int peek () {
        if (head == null) {
            throw new RuntimeException();
        } else {
            return head.val;
        }
    }

    public int size () {
        int size = 0;
        Node curNode = head;
        while (curNode != null) {
            size++;
            curNode = curNode.next;
        }
        return size;
    }

    public boolean isEmpty () {
        if (head == null) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.offer(1);
        myQueue.offer(2);
        myQueue.offer(3);
        myQueue.offer(4);
        myQueue.offer(5);
        System.out.println(myQueue.size());
        System.out.println(myQueue.peek());

        myQueue.poll();
        System.out.println(myQueue.poll());

        if (myQueue.isEmpty()) {
            System.out.println("队列空");
        } else {
            System.out.println(myQueue.size());
        }

    }
}

class MyCircularQueue {

    int usedSize;
    int[] elems;
    int front;
    int rear;

    public MyCircularQueue(int k) {
        elems = new int[k];
        usedSize = 0;
        front = 0;
        rear = 0;
    }

    public boolean enQueue(int value) {
        //空数组front和rear的值不变，原地添加
        if (isEmpty()) {
            front = rear;
            elems[front] = value;
            usedSize++;
            return true;
        }
        //数组非空非满，队尾添加
        else if (!isEmpty() && !isFull()) {
            rear++;
            rear %= elems.length;
            elems[rear] = value;
            usedSize++;
            return true;
        }
        //数组已满，添加失败
        else {
            return false;
        }
    }

    public boolean deQueue() {
        //非空，删除队首元素
        //本质上没有删除，但是front后移，前一个数据无法直接读取
        //且usedSize减少
        if (!isEmpty()) {
            front++;
            front %= elems.length;
            usedSize--;
            return true;
        }
        //数组空，删除失败
        return false;
    }

    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return elems[front];
    }

    public int Rear() {
        //空就返回-1
        if (isEmpty()) {
            return -1;
        }
        //非空就返回最后一个rear下标的值
        return elems[rear];
    }

    public boolean isEmpty() {
        if (usedSize == 0) {
            return true;
        }
        return false;
    }

    public boolean isFull() {
        if (usedSize == elems.length) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        MyCircularQueue myCircularQueue = new MyCircularQueue(2);
        myCircularQueue.enQueue(8);
        myCircularQueue.enQueue(8);
        myCircularQueue.Front();
        myCircularQueue.enQueue(4);
        myCircularQueue.deQueue();
        myCircularQueue.enQueue(1);
        myCircularQueue.enQueue(1);
        myCircularQueue.Rear();
        myCircularQueue.isEmpty();
        int n = myCircularQueue.Front();
        System.out.println(n);
        myCircularQueue.deQueue();
    }
}
